package com.foreveross.chameleon.config;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Environment;

import com.foreveross.bsl.test.R;
import com.foreveross.chameleon.utils.PropertiesUtil;

/**
 * @description 变色龙全局配置类
 * @author zhouzhineng
 *
 */
public class ChameleonConfig {

	public final static int CUBE_RAW = R.raw.cube;
	
	public final static String FILE_SCHEME = "file://";
	/**
	 * 接口基地址
	 */
	public static String BASE_URL;
	
	/**
	 * 接口环境选择
	 */
	public static String PROJECT;
	
	/**
	 * app 鉴权地址
	 */
	public static String APP_AUTH_URL;
	
	/**
	 * 模块同步地址
	 */
	public static String MODULE_SYNC_URL;
	
	/**
	 * app同步地址
	 */
	public static String APP_SYNC_URL;
	
	public static String APP_DOWNLOAD_COUNT_UPDATE;
	/**
	 * APP ID
	 */
	public static String APP_ID;
	
	public static String APP_TOKEN;
	
	/**
	 * APP secret
	 */
	public static String APP_SECRET;
	
	public static int VERSION_CODE;
	
	public static String VERSION_NAME;
	
	public static String APP_PACKAGENAME;
	
	/**
	 * app resource dir in sd card
	 */
	public static String APP_PATH;
	
	public static String PAD_MAIN_URL;
	
	public static String PHONE_MAIN_URL;
	
	public static boolean NETWORK_OK = true;
	
	public static String REMOTE_MAIN_MODULE_TAG;		//记录最新主模块标识
	
	public static boolean APP_VALID = true;				//应用合法性
	
	/**
	 * load config from cube.properties
	 * @param ctx
	 */
	public static void loadConfigAndInit(Context ctx) {
		BASE_URL = getPropValue(ctx,ChameleonConstants.BASE_URL);
		PROJECT = getPropValue(ctx,ChameleonConstants.PROJECT);
		APP_AUTH_URL = BASE_URL + PROJECT + "/api/v1/anonymous/auth";
		APP_DOWNLOAD_COUNT_UPDATE = BASE_URL + PROJECT + "/api/v1/anonymous/add_download_amount_build";
		APP_ID = getPropValue(ctx,ChameleonConstants.APP_ID);
		APP_SECRET = getPropValue(ctx,ChameleonConstants.APP_SECRET);
		PackageInfo info;
		try {
			info = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0);
			VERSION_CODE = info.versionCode;
			VERSION_NAME = info.versionName;
			APP_PACKAGENAME = info.packageName;
			APP_PATH = Environment.getExternalStorageDirectory() + "/" + APP_PACKAGENAME;
			PAD_MAIN_URL = FILE_SCHEME + APP_PATH + "/www/" + getPropValue(ctx,ChameleonConstants.PAD_MAIN_URL);
			PHONE_MAIN_URL = FILE_SCHEME + APP_PATH + "/www/" + getPropValue(ctx,ChameleonConstants.PHONE_MAIN_URL);
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/**
	 * add config when auth completed
	 */
	public static void addConfigAfterAppAuth(String token) {
		APP_TOKEN = token;
		APP_SYNC_URL = BASE_URL + PROJECT + "/api/v1/anonymous/update_app/" + APP_ID + "/ANDROID/" + APP_TOKEN;
		MODULE_SYNC_URL = BASE_URL + PROJECT + "/api/v1/anonymous/update_module/" + APP_ID +"/ANDROID/" + APP_TOKEN + "/" + VERSION_NAME;
	}
	
	/**
	 * get property value from cube.properties
	 * @param ctx
	 * @param key
	 * @return
	 */
	private static String getPropValue(Context ctx,String key) {
		return PropertiesUtil.readProperties(ctx,
				ChameleonConstants.RAW_CONFIG).getString(key, "");
	}
	
}
